home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / program / fnum.zip / FNUM.H < prev   
C/C++ Source or Header  |  1996-07-23  |  6KB  |  108 lines

  1. //FNUM.H - advanced C++ number handling functions.
  2. /*
  3. started 04/30/96 by Richard Springstead.  copyright waived.
  4. The purpose of the iofnum functions is the integration of advanced number storing
  5. functions with the iofstream group.
  6. 06/25/96 - during a sudden brainstorm I envisioned a way to standardize the functions
  7. all into a single class called FNUM, which also allowed me to place all functions
  8. into the class description as inlines! I was most impressed with myself.
  9. To use these functions I suggest declaring a global FNUM class at the begining of the source file
  10. and from there it is simple.
  11. (i.e
  12. #include "fstream.h"
  13. #include "iofnum.h"
  14.  
  15. FNUM Number;
  16. void main(void) {
  17.     int n = 5;    // this being the desired integer to write to disk
  18.     ofstream output;  //C++ V2.0 stream class
  19.     output. open("filename.ext", ios::binary); // opens in binary mode (or should).
  20.     Number.out(n, output); // calls the overloaded FNUM class function out
  21.     output.close();        // close the file (should also flush the buffer.
  22.     ifstream input;        // establish an input stream.
  23.     input.open("filename.ext", ios::binary); // connect input stream to a file.
  24.     Number.in(n, input); // call overloaded FNUM in function.
  25.     input.close(); // close the file steam (and should flush buffer).
  26.  
  27. }
  28.  
  29. )
  30. the out and in are overloaded to handle all standard data types.
  31. I may add a string in/out later (which I have).  I should be easy to add
  32. any desired datatype not listed.
  33.  
  34. the advantages of FNUM:
  35.     FNUM uses what I call "memory image" storage of numeric variables, often reducing the space
  36. required to store those values as compared to ascii in most situations, and eliminates the
  37. need to convert the complex numbers to and from ascii.
  38.     All the functions should prove to be reasonably fast and consume very
  39. little memory.
  40.  
  41. and the disadvantages of FNUM:
  42.     The biggest being that these functions DO NOT CHECK for file errors, you'll have to
  43. either do these seperately, or if you design a standard error checking routine,
  44. it should be a simple thing to have eash function call it.
  45.     Files written/read in text mode may cause problems due to the
  46. newline/carriage return character striking.
  47. this can be prevented by openning the stream in binary mode -
  48. ofstream output;
  49. output.open("filename.ext", ios::binary);
  50. - like so.
  51.  
  52.  
  53. 7/18/96 - At this point I have decided to add a few special additions
  54.     basically the get/put for Null Terminated strings.
  55. */
  56.  
  57. typedef unsigned char BYTE;
  58.  
  59. class FNUM {
  60.     char far *address;
  61.  
  62.     public:
  63. void out(char c, ofstream far& output) { output.put(c); }
  64. void out(unsigned char c, ofstream far & output) {output.put(c);}
  65. void out(int n, ofstream far& output) { address = (char *)&n; output << address[0] << address[1];}
  66. void out(unsigned int un, ofstream far& output) { address = (char *)&un; output << address[0] << address[1]; }
  67. void out(long ln, ofstream far& output) { address = (char *)&ln; output << address[0] << address[1] << address[2] << address[3]; }
  68. void out(unsigned long uln, ofstream far& output) { address = (char *)&uln; output << address[0] << address[1] << address[2] << address[3]; }
  69. void out(float fl, ofstream far& output) { address = (char *)&fl; output << address[0] << address[1] << address[2] << address[3]; }
  70. void out(double dn, ofstream far& output) { address = (char *)&dn; output << address[0] << address[1] << address[2] << address[3] << address[4] << address[5] << address[6] << address[7]; }
  71. void out(char far *nzascii, ofstream far& output) { output << nzascii << endl;}
  72.  
  73. void in(char c, ifstream far& input) { input.get(c); }
  74. void in(unsigned char c, ifstream far& input) {input.get(c); }
  75. void in(int  n,ifstream far& input) { address = (char *)&n; input >> address[0] >> address[1]; }
  76. void in(unsigned int un, ifstream far& input) { address = (char *)&un; input >> address[0] >> address[1]; }
  77. void in(long ln, ifstream far& input) { address = (char *)&ln; input >> address[0] >> address[1] >> address[2] >> address[3]; }
  78. void in(unsigned long uln, ifstream far& input) { address = (char *)&uln; input >> address[0] >> address[1] >> address[2] >> address[3]; }
  79. void in(double dn, ifstream far& input) { address = (char *)&dn; input >> address[0] >> address[1] >> address[2] >> address[3]; }
  80. void in(float fl, ifstream far& input) { address = (char *)&fl; input >> address[0] >> address[1] >> address[2] >> address[3] >> address[4] >> address[5] >> address[6] >> address[7]; }
  81. void in(char far *nzascii, int len, char delim = '\n', ifstream far& input) { input.get(nzascii, len, delim); }
  82. };
  83.  
  84. /*  FNUM author's final notes -
  85. FNUM is presented to you as is and free of charge, basically because I feel
  86. that what it contains is not above any programmer's capability (it's that
  87. simple) and that it may prove an interresting learming tool into the
  88. bizzare mysteries of C plus plus programming.  I have been studying C++
  89. for almost a year now, and I am still in awe of the language.
  90.  
  91. The functions presented here may be used as desired, with neither remitance
  92. nor credit being required by the author (me, Richard Springstead) who also
  93. will not accept any liability for any damage caused, consiquential or otherwise,
  94. by the use or inclusion of these functions, in other words, use at your own risk!
  95.  
  96. I present FNUM to the programming world at large as a message in a bottle,
  97. hoping that in the great vastness of our world another will see and respond.
  98. If you wish to send me any comments, idea, critiques (no abuse, please),
  99. or just a friendly how-do I can be contacted in the following ways.
  100.  
  101. E-mail:
  102. richard.springstead@dconline.org
  103.  
  104. or
  105. US MAIL:
  106. Richard Springstead
  107. 142 S. Jefferson St.
  108. Coldwater, MI 49036